package com.example.demo.util;

public class NumberToCN {
    
    //自定义字符
    private static final String ling = "零";  
    private static final String[] zheng = {"圆","整"};  

    //定义大写中文数组
    private static final String[] cnArr = 
//            { "零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖" };
            { "零", "一", "二", "三", "四", "五", "六", "七", "八", "九" };

    //自定义单位数组
    private static final String[] danwei =
            {"", "拾", "佰", "仟" , "万" ,"拾" , "佰" ,"仟" , "亿" };
    private static final String[] units  = {"角","分"}; 
    
    //自定义转换方法
    public static String convert(String str){//168.789
        String xiaoshu = null;//小数
        String zhengshu = null;//整数
        String result = null;//拼接结果
        
        if(str!=null && str!=""){
            //转换double
            double d1 = Double.parseDouble(str);//168.789
            if(d1 > 0){
                //四舍五入保留两位小数
                String s1 = String.format("%.2f", d1);//168.79
                //整数小数分离
                String[] arr = s1.split("[.]");//["168","79"]
                for (int i = 0; i < arr.length; i++) {
                    if(i<1){
                        zhengshu = arr[i];
                    }else{
                        xiaoshu = arr[i];
                    }
                }
                
                //处理小数
                String xiaoshu1 = null ;
                if("00".equals(xiaoshu)){
                    //输入的为一个整数时
                    xiaoshu = zheng[1];//圆整
                }else{
                    for (int i=0;i<xiaoshu.length();i++){
                        if(i<1){
                            xiaoshu1 = charToCn(xiaoshu.
                                    charAt(i))+units[0];//柒角
                        }else{
                            xiaoshu1 += charToCn(xiaoshu.
                                    charAt(i))+units[1];//玖分
                        }
                    }
                    xiaoshu = xiaoshu1;//柒角玖分
                }
                System.out.println(">>>小数部分---"+xiaoshu);
                
                //处理整数
                String zhengshu1 = "";
                if(zhengshu.length()>13){
                    return "输入的数字过大！";
                }else{
                    for (int i = 0; i < zhengshu.length(); i++) {
                        if("零".equals(charToCn(zhengshu.charAt(i)))){
                            //为0省略单位
                            zhengshu1+="零";
                        }else{
                            zhengshu1 += charToCn(zhengshu.charAt(i))+
                                    danwei[zhengshu.length()-1-i];//壹佰陆拾捌
                        }
                    }
                    zhengshu = zhengshu1+zheng[0];//壹佰陆拾捌圆
                }
                System.out.println(">>>整数部分---"+zhengshu);
                
                //拼接结果
                result = zhengshu + xiaoshu;
                System.out.println(">>>拼接结果---"+result);
                
            }else{
                return "输入的数字不合法！";
            }
            return result;
        }else{
            return "请输入一个数字";
        }
    }
    //char转整int,再转String
    public static String charToCn(char c){
        String result = null;
        int c1 =  Integer.parseInt(String.valueOf(c));
        for (int i = 0; i < cnArr.length; i++) {
            if(i == c1){
                result = cnArr[i];
            }
        }
        return result;
    }

    //char转整int,再转String
    public static String integerToCn(Integer num){
        String result= "";
        for (int i = 0; i < cnArr.length; i++) {
            if(i == num){
                result = cnArr[i];
            }
        }
        return result;
    }
    public static void main(String[] args) {
        //初始化输入值
        String str = "168.789";
        convert(str);
    }
}